package cn.ekuma.impexp.ui.swing;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import javax.swing.BoxLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;


//Tudo 把相关类外移
public class JStartEndDateSelectPanel extends JPanel {
    private JTextField jTxtStartDate;
    private JTextField jTxtEndDate;
    private static DateFormat m_datetimeformat = DateFormat.getDateTimeInstance();

    /**
     * Create the panel.
     */
    public JStartEndDateSelectPanel() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        JLabel label = new JLabel("时间启");
        add(label);

        jTxtStartDate = new JTextField();
        jTxtStartDate.setColumns(10);
        add(jTxtStartDate);

        JButton btnStartTime = new JButton("");
        btnStartTime.setIcon(new ImageIcon(JStartEndDateSelectPanel.class.getResource("/com/openbravo/images/date.png")));
        btnStartTime.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnDateStartActionPerformed(e);
            }
        });
        add(btnStartTime);

        JLabel label_1 = new JLabel("时间止");
        add(label_1);

        jTxtEndDate = new JTextField();
        jTxtEndDate.setColumns(10);
        add(jTxtEndDate);

        JButton btnEndTime = new JButton("");
        btnEndTime.setIcon(new ImageIcon(JStartEndDateSelectPanel.class.getResource("/com/openbravo/images/date.png")));
        btnEndTime.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnDateEndActionPerformed(e);
            }
        });
        add(btnEndTime);
    }

    private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDateStartActionPerformed
        Date date;
        try {
            date = m_datetimeformat.parse(jTxtStartDate.getText());
        } catch (ParseException e) {
            date = null;
        }
        //date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtStartDate.setText(m_datetimeformat.format(date));
        }
    }// GEN-LAST:event_btnDateStartActionPerformed

    private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDateEndActionPerformed
        Date date;
        try {
            date = m_datetimeformat.parse(jTxtEndDate.getText());
        } catch (ParseException e) {
            date = null;
        }
        //date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtEndDate.setText(m_datetimeformat.format(date));
        }
    }// GEN-LAST:event_btnDateEndActionPerformed


    public Date getStartDate(){
        Date date;
        try {
            date = m_datetimeformat.parse(jTxtStartDate.getText());
        } catch (ParseException e) {
            date = null;
        }
        return date;
    }

    public Date getEndDate(){
        Date date;
        try {
            date =  m_datetimeformat.parse(jTxtEndDate.getText());
        } catch (ParseException e) {
            date = null;
        }
        return date;
    }
}